import Vue from 'vue';
import VueRouter from 'vue-router';
// 导入Login组件
import Login from '../components/Login.vue';
// 导入Welcome组件
import Welcome from '../components/Welcome.vue';

Vue.use(VueRouter);

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/welcome', component: Welcome }
];

const router = new VueRouter({
  routes
});
// 路由导航门卫
router.beforeEach((to, from, next) => {
  // 是否为跳转到/login,则放行
  if (to.path == '/login') return next();
  // 不是跳转到/login
  // 分两种情况:
  // 1.没携带token令牌，回到/login登录页
  // 2.携带了token令牌，放行
  const token = sessionStorage.getItem('token');
  if (!token) return next('/login');
  next();
});
export default router;
